/*
Copyright SecureKey Technologies Inc. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package store

import (
	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/ichain/keyvaluestore"
	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/providers/core"
	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/providers/iam"
	"github.com/pkg/errors"
)

func NewFileCertStore(cryptoConfigIAMPath string) (core.KVStore, error) {
	opts := &keyvaluestore.FileKeyValueStoreOptions{
		Path: cryptoConfigIAMPath,
		KeySerializer: func(key interface{}) (string, error) {
			ck, ok := key.(*iam.IdentityIdentifier)
			if !ok {
				return "", errors.New("converting key to CertKey failed")
			}
			if ck == nil || ck.IAMID == "" || ck.ID == "" {
				return "", errors.New("invalid key")
			}
			path, err := cryptoConfigCertOrKeyPath(cryptoConfigIAMPath, ck.ID, FILE_SUFFIX_PEM)
			if err != nil {
				return "", err
			}
			return path, nil
		},
	}
	return keyvaluestore.New(opts)
}
